using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;
using Common.Utilities.DLR;

namespace Workflows.Components.Rules
{
    public partial class RuleComponentEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private RuleComponent _Rule;
        private Dictionary<string, List<Rule>> _RulesByCategories;
        /// <summary>
        /// 
        /// </summary>
        public RuleComponentEditor()
        {
            InitializeComponent();
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(RuleComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._Rule = (RuleComponent) wfComponent;
        }

        public void SaveSettings()
        {
            // TODO: implement test
        }

        public TestResult Test()
        {
            // TODO: implement test
            return null;
        }

        private void RuleComponentEditor_Load(object sender, EventArgs e)
        {
            this._RulesByCategories=new Dictionary<string, List<Rule>>();
            ORMapping<Rule> orm=new ORMapping<Rule>();
            List<Rule> rules = orm.GetAll(ConfigurationManager.ConnectionStrings["Workflow"].ConnectionString);
            if(rules !=null && rules.Count>0)
            {
                foreach(Rule rule in rules)
                {
                    if(this._RulesByCategories.ContainsKey(rule.RuleCategory))
                    {
                        List<Rule> ruleList = this._RulesByCategories[rule.RuleCategory];
                        ruleList.Add(rule);
                        this._RulesByCategories[rule.RuleCategory] = ruleList;
                    }
                    else
                    {
                        List<Rule> ruleList=new List<Rule>();
                        ruleList.Add(rule);
                        this._RulesByCategories.Add(rule.RuleCategory,ruleList);
                    }
                }
            }

            this.cbo_RuleCategories.Items.Clear();
            if(this._RulesByCategories !=null && this._RulesByCategories.Count>0)
            {
                foreach(string category in this._RulesByCategories.Keys)
                {
                    this.cbo_RuleCategories.Items.Add(category);
                }
                this.cbo_RuleCategories.SelectedIndex = 0;
            }
        }

        private void cbo_RuleCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbo_RuleCategories.SelectedIndex >= 0)
            {
                string category = this.cbo_RuleCategories.SelectedItem.ToString();
                List<Rule> ruleList = this._RulesByCategories[category];
                this.cbo_Rules.Items.Clear();
                foreach(Rule rule in ruleList)
                {
                    this.cbo_Rules.Items.Add(rule.RuleName);
                }
                this.cbo_Rules.SelectedIndex = 0;
            }
        }

        private void cbo_Rules_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_RuleCategories.SelectedIndex >=0 && this.cbo_Rules.SelectedIndex>=0)
            {
                string category = this.cbo_RuleCategories.SelectedItem.ToString();
                List<Rule> ruleList = this._RulesByCategories[category];
                string ruleName = this.cbo_Rules.SelectedItem.ToString();
                foreach(Rule rule in ruleList)
                {
                    if(rule.RuleName==ruleName)
                    {
                        this.txt_RuleID.Text = rule.RuleID.ToString();
                        this.txt_Author.Text = rule.CreatedBy;
                        this.txt_CreateDate.Text = rule.CreatedOn.ToString();
                        string assemblyName = string.Empty;
                        if(!string.IsNullOrEmpty(rule.AssemblyFilePath))
                        {
                            assemblyName = Path.GetFileName(rule.AssemblyFilePath);
                        }
                        string boTypeName = string.Empty;
                        if(!string.IsNullOrEmpty(rule.BusinessObjectName))
                        {
                            boTypeName = rule.BusinessObjectName;
                        }
                        this.txt_BO.Text = assemblyName + ":" + boTypeName;
                        StringBuilder buffer=new StringBuilder();
                        if(rule.Vars !=null && rule.Vars.Length>0)
                        {
                            foreach(GlobalVariable var in rule.Vars)
                            {
                                buffer.Append(var.VarName + Environment.NewLine);
                            }
                        }
                        this.txt_ReturnValues.Text = buffer.ToString();
                        break;
                    }
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._Rule);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}